import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:just_audio/just_audio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zz_listen_english/common/index.dart';
import 'package:zz_listen_english/common/utils/background_service_magager.dart';
import 'common/api/http_head.dart';
import 'common/services/wp_http.dart';
import 'common/utils/audio_play_manager.dart';
import 'global.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Global.init();
  runApp(const MyApp());
//  await initializeService();
  final backgroundService=  BackgroundServiceManager();
  backgroundService.initializeService();
}

// Future<void> initializeService() async {
//   final service = FlutterBackgroundService();
//   await service.configure(
//     androidConfiguration: AndroidConfiguration(
//       // 此服务将注册为 "io.flutter.plugins.backgroundservice.BackgroundService"
//       // 你可以通过这个名称在 AndroidManifest.xml 中配置权限
//       onStart: onStart,
//       autoStart: true,
//       isForegroundMode: true,
//     ),
//     iosConfiguration: IosConfiguration(),
//   );
// }
//
// @pragma('vm:entry-point')
// void onStart(ServiceInstance service) async {
//   // 设置周期性任务
//   final audioPlayerManager = AudioPlayerManager();
//   audioPlayerManager.setAudioUrls([
//          'http://english-1234.oss-cn-beijing.aliyuncs.com/%E7%8E%8B%E6%9D%A8%E9%9D%92_1764841162285182978/xsbb/%E8%A7%86%E9%A2%91/MaisyE003Pool/MaisyE003Pool.mp3'
//   ]);
//   final Dio dio = Dio();
//   final prefs = await SharedPreferences.getInstance();
//   String? studyPlanId = prefs.getString("studyPlanId");
//   String httpHead = "${prefs.getString(Constants.prefixIp)}://${prefs.getString(
//       Constants.ip)}:${prefs.getString(Constants.port)}";
//   Timer.periodic(Duration(seconds: 20), (timer) async {
//     print("定时器已打开");
//     try {
//       await audioPlayerManager.play();
//       // 加载音频文件
//       // await audioPlayer.setUrl(
//       //     'http://english-1234.oss-cn-beijing.aliyuncs.com/%E7%8E%8B%E6%9D%A8%E9%9D%92_1764841162285182978/xsbb/%E8%A7%86%E9%A2%91/MaisyE003Pool/MaisyE003Pool.mp3');
//       // // 开始播放音频
//       // audioPlayer.play();
//       // 定义自定义头部信息
//       final customHeaders = {
//         'Content-Type': 'application/json',
//         'Authorization': 'Bearer ${prefs.getString(Constants.storageToken)}',
//       };
//       final params = {
//         "id": studyPlanId == "" ? 0 : studyPlanId,
//         "playFlag": 2
//       };
//       final response = await dio.get(
//           httpHead + "/plStudySchedule/checkAudioPlay",
//           options: Options(headers: customHeaders),
//       queryParameters: params);
//       print(response.toString() + "00000000000000000000");
//     } catch (e) {
//       print("Error: $e");
//     }
//   });
// }

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize:
        const Size(1800, 1080), // 设计稿中设备的尺寸(单位随意,建议dp,但在使用过程中必须保持一致)
        splitScreenMode: false, // 支持分屏尺寸
        minTextAdapt: false, // 是否根据宽度/高度中的最小值适配文字
        // 一般返回一个MaterialApp类型的Function()
        builder: (context, child) {
          return GetMaterialApp(
            title: 'Flutter Demo',
            initialRoute: RouteNames.splash,
            getPages: RoutePages.list,
            debugShowCheckedModeBanner: false,
            builder: (context, widget) {
              widget = EasyLoading.init()(context, widget); // EasyLoading 初始化
              // 不随系统字体缩放比例
              return MediaQuery(
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                child: widget,
              );
            },
          );
        });
  }
}
